﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Configuration;

/// <summary>
///SpecialCharHandler 的摘要说明
/// </summary>
public class SpecialCharHandler
{
    public SpecialCharHandler()
    { }
    /// <summary>
    /// HTML特殊字符处理
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string toHTMLString(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            string c = str.Substring(i, 1);
            switch (c)
            {
                case "&":
                    sb.Append("&amp;");
                    break;
                //case " ":
                //    sb.Append("&nbsp;");
                //    break;
                case "\"":
                    sb.Append("&quot;");
                    break;
                case "'":
                    sb.Append("&#39;");
                    break;
                case ">":
                    sb.Append("&gt;");
                    break;
                case "<":
                    sb.Append("&lt;");
                    break;
                default:
                    sb.Append(c);
                    break;
            }
        }

        return sb.ToString();
    }

    /// <summary>
    /// JS特殊字符处理
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string toJSString(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            string c = str.Substring(i, 1);
            switch (c)
            {
                case "\"":
                    sb.Append("\\\"");
                    break;
                case "'":
                    sb.Append(@"\'");
                    break;
                default:
                    sb.Append(c);
                    break;
            }
        }

        return sb.ToString().Replace("\r\n", "<br>");
    }

    /// <summary>
    /// HTML + JS 特殊字符处理
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string toHTMLJSString(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            string c = str.Substring(i, 1);
            switch (c)
            {
                case "&":
                    sb.Append("&amp;");
                    break;
                //case " ":
                //    sb.Append("&nbsp;");
                //    break;
                case "\"":
                    sb.Append(@"\&quot;");
                    break;
                case "'":
                    sb.Append(@"\&#39;");
                    break;
                case ">":
                    sb.Append("&gt;");
                    break;
                case "<":
                    sb.Append("&lt;");
                    break;
                default:
                    sb.Append(c);
                    break;
            }
        }

        return sb.ToString().Replace("\r\n", "<br>");
    }

    /// <summary>
    /// Json 特殊字符处理
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string toJsonString(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            string c = str.Substring(i, 1);
            switch (c)
            {
                case "\"":
                    sb.Append("\\\"");
                    break;
                default:
                    sb.Append(c);
                    break;
            }
        }

        return sb.ToString();
    }

    /// <summary>
    /// 替换html中的特殊字符
    /// </summary>
    /// <param name="theString">需要进行替换的文本。</param>
    /// <returns>替换完的文本。</returns>
    public static string HtmlEncode(string theString)
    {
        theString = theString.Replace("&", "&amp;");
        theString = theString.Replace(">", "&gt;");
        theString = theString.Replace("<", "&lt;");
        theString = theString.Replace(" ", "&nbsp;");
        theString = theString.Replace("　", "&nbsp1;");
        theString = theString.Replace("\"", "&quot;");
        theString = theString.Replace("\'", "'");
        theString = theString.Replace("\n", "<br/> ");  
        theString = theString.Replace("'", @"\&#39;");
        theString = theString.Replace("'", "''");
        return theString;
    }

    /// <summary>
    /// 恢复html中的特殊字符
    /// </summary>
    /// <param name="theString">需要恢复的文本。</param>
    /// <returns>恢复好的文本。</returns>
    public static string HtmlDiscode(string theString)
    {
        theString = theString.Replace("&amp;", "&");
        theString = theString.Replace("&gt;", ">");
        theString = theString.Replace("&lt;", "<");
        theString = theString.Replace("&nbsp;", " ");
        theString = theString.Replace("&nbsp1;", "　");
        theString = theString.Replace("&quot;", "\"");
        theString = theString.Replace("'", "\'");
        theString = theString.Replace("<br/> ", "\n");        
        theString = theString.Replace(@"\&#39;", "'");
        theString = theString.Replace("''", "'");
        return theString;
    }
}